package com.eascs.afterloan.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.afterloan.entity.O2oLoanapplyOverdue;
import com.eascs.afterloan.form.O2oLoanapplyOverdueForm;
import com.eascs.afterloan.service.O2oLoanapplyOverdueService;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@RestController
@RequestMapping("/ro2oloanoverdue")
public class O2oLoanapplyOverdueRestController extends BaseRestController {
	@Autowired
	O2oLoanapplyOverdueService service;

	@RequestMapping("/save")
	public BaseRestResult<String> save(O2oLoanapplyOverdueForm form) {
		String checkResult = check(form);
		if (!"SUCCESS".equals(checkResult)) {
			return BaseRestResult.warn(checkResult);
		}
		//loan-13039 贷款单逾期信息维护功能，要能支持同一笔贷款单录入多条数据 
		/*O2oLoanapplyOverdue loanapplyOverdueCheck = service.queryByLoanapplyNo(form.getLoanapplyNo());
		if (null != loanapplyOverdueCheck) {
			return BaseRestResult.warn("该单号已经在逾期信息维护中，不能再添加！");
		}*/
		O2oLoanapplyOverdue loanapplyOverdue = new O2oLoanapplyOverdue();
		loanapplyOverdue.setBillCode(form.getBillCode());
		loanapplyOverdue.setLoanapplyNo(form.getLoanapplyNo());
		loanapplyOverdue.setLoanUserName(form.getLoanUserName());
		loanapplyOverdue.setProvince(form.getProvince());
		loanapplyOverdue.setLoanBankName(form.getLoanBankName());
		loanapplyOverdue.setLoanOperer(form.getLoanOperer());
		loanapplyOverdue.setOverdueTotal(form.getOverdueTotal());
		loanapplyOverdue.setOverduePrincipal(form.getOverduePrincipal());
		loanapplyOverdue.setOverdueInterest(form.getOverdueInterest());
		loanapplyOverdue.setPenaltyTotal(form.getPenaltyTotal());
		loanapplyOverdue.setOverdueTime(form.getOverdueTime());
		User user = UserHolder.getUser();
		if (null == user) {
			user = new User();
			user.setNo("1");
			user.setCname("系统管理员");
		}
		loanapplyOverdue.setCreateTime(new Date());
		loanapplyOverdue.setIsDelete(StatusContexts.O2O_NO);
		loanapplyOverdue.setCreateUserNo(user.getNo());
		loanapplyOverdue.setCreateUserName(user.getCname());
		int i = service.insert(loanapplyOverdue);
		if (i > 0) {
			return BaseRestResult.info("保存成功").andRedirect("/o2oloanoverdue/index");
		}
		return BaseRestResult.warn("保存失败");
	}

	@RequestMapping("/update")
	public BaseRestResult<String> update(O2oLoanapplyOverdueForm form, String loanapplyNoCheck) {
		String checkResult = check(form);
		if (!"SUCCESS".equals(checkResult)) {
			return BaseRestResult.warn(checkResult);
		}
		//loan-13039 贷款单逾期信息维护功能，要能支持同一笔贷款单录入多条数据 
		/*if (!loanapplyNoCheck.equals(form.getLoanapplyNo())) {
			O2oLoanapplyOverdue loanapplyOverdueCheck = service.queryByLoanapplyNo(form.getLoanapplyNo());
			if (null != loanapplyOverdueCheck) {
				return BaseRestResult.warn("该单号已经在逾期信息维护中，不能再添加！");
			}
		}*/
		O2oLoanapplyOverdue loanapplyOverdue = service.queryById(form.getId());
		loanapplyOverdue.setBillCode(form.getBillCode());
		loanapplyOverdue.setLoanapplyNo(form.getLoanapplyNo());
		loanapplyOverdue.setLoanUserName(form.getLoanUserName());
		loanapplyOverdue.setProvince(form.getProvince());
		loanapplyOverdue.setLoanBankName(form.getLoanBankName());
		loanapplyOverdue.setLoanOperer(form.getLoanOperer());
		loanapplyOverdue.setOverdueTotal(form.getOverdueTotal());
		loanapplyOverdue.setOverduePrincipal(form.getOverduePrincipal());
		loanapplyOverdue.setOverdueInterest(form.getOverdueInterest());
		loanapplyOverdue.setPenaltyTotal(form.getPenaltyTotal());
		loanapplyOverdue.setOverdueTime(form.getOverdueTime());
		User user = UserHolder.getUser();
		if (null == user) {
			user = new User();
			user.setNo("1");
			user.setCname("系统管理员");
		}
		loanapplyOverdue.setCreateTime(new Date());
		loanapplyOverdue.setIsDelete(StatusContexts.O2O_NO);
		loanapplyOverdue.setCreateUserNo(user.getNo());
		loanapplyOverdue.setCreateUserName(user.getCname());
		loanapplyOverdue.setUpdateTime(new Date());
		int i = service.updateById(loanapplyOverdue);
		if (i > 0) {
			return BaseRestResult.info("保存成功").andRedirect("/o2oloanoverdue/index");
		}
		return BaseRestResult.warn("保存失败");
	}

	@RequestMapping("/delete")
	public BaseRestResult<String> delete(String id) {
		O2oLoanapplyOverdue loanapplyOverdue = service.queryById(id);
		loanapplyOverdue.setIsDelete(StatusContexts.O2O_YES);
		loanapplyOverdue.setUpdateTime(new Date());
		int i = service.updateById(loanapplyOverdue);
		if (i > 0) {
			return BaseRestResult.info("删除成功").andRefresh();
		}
		return BaseRestResult.warn("删除失败");
	}

	@RequestMapping("/queryByBillCode")
	public BaseRestResult<List<Object[]>> queryByBillCode(String billCode) {
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(billCode)) {
			data = service.queryList(billCode);
		}
		return BaseRestResult.buildRestResult(data);
	}

	public String check(O2oLoanapplyOverdueForm form) {
		if (StringUtil.isBlank(form.getBillCode())) {
			return "贷款单号不能为空！";
		}
		if (null == form.getOverdueTotal()) {
			return "逾期总额不能为空！";
		}
		if (null == form.getOverduePrincipal()) {
			return "逾期本金不能为空！";
		}
		if (null == form.getOverdueInterest()) {
			return "逾期利息不能为空！";
		}
		if (null == form.getPenaltyTotal()) {
			return "罚息合计不能为空！";
		}
		if (null == form.getOverdueTime()) {
			return "逾期发生时间不能为空！";
		}

		return "SUCCESS";
	}

}
